<template>
    <div
        class="list-container">
        <van-sticky>
            <div
                class="list-header-box">
                <div class="list-tab-box">
                    <div class="list-tab">
                        <div
                            v-for="item in tabArr"
                            :key="item.value"
                            @click="chooseTab(item.value)"
                            :class="['tab', currentTab == item.value ? 'active':'']">
                            {{item.label}}
                        </div>
                    </div>
                    <div
                        v-show="currentTab == 'all'"
                        class="list-search"
                        @click="clickSearchShow">
                        <wp-icon name="search" />
                        {{ $t('wp-sou-suo') }}
                    </div>
                </div>
                <div class="list-filter">
                    <div
                        class="filter-select"
                        @click="clickCustomerShow()">
                        <div
                            v-if="chooseCustomer"
                            class="select-cutomer">
                            <div>{{ chooseCustomer.customer_name }}</div>
                            <wp-icon
                                @click.stop="delCustomer"
                                name="shut" /></div>
                        <div
                            v-else
                            class="select-cutomer">
                            {{ $t('wp-suo-shu-ke-hu') }}
                            <wp-icon name="pulldown" /></div>
                    </div>
                    <div
                        @click="areaPopopShow"
                        class="filter-select">
                        <div
                            class="select-area"
                            v-if="chooseArea">
                            <div>{{ chooseArea }}</div>
                            <wp-icon
                                @click.stop="delArea"
                                name="shut" /></div>
                        <div
                            v-else
                            class="select-area">{{ $t('wp-di-qu') }}<wp-icon name="pulldown" /></div>
                    </div>
                </div>
            </div>
        </van-sticky>
        <wp-null-data-area
            :null-data-msg="$st('wp_No_data')"
            v-if="listData.length === 0"
            null-text-msg-color="rgba(0,0,0,0.6)"
        />
        <div
            v-if="listData.length >  0"
            class="list-content">
            <van-list
                @load="onLoad"
                v-model:loading="loading"
                :finished="finished"
                :finished-text="$st('wp_No_more')"
                :immediate-check="false">
                <template
                    v-for="(item, index) in listData">
                    <card
                        v-if="item"
                        class="list-card"
                        :key="index">
                        <template v-slot:title>
                            <div class="title-box">
                                <device-status
                                    :round="true"
                                    :uuid="item.uuid"></device-status>
                                <div class="title">{{ item.devalias}}</div>
                            </div>
                        </template>
                        <div class="line-box">
                            <div class="line-left">{{ $t('wp-she-bei-zhuang-tai') }}</div>
                            <div class="line-right">
                                <device-status
                                    :uuid="item.uuid">
                                </device-status>
                            </div>
                        </div>
                        <line-cell
                            :listSetProps="listprops"
                            :listData="item">
                        </line-cell>
                        <div class="opt-box">
                            <van-button
                                plain
                                color="#1890ff"
                                @click="toDetail(item)"
                                class="btn">{{ $t('wp-xiang-qing') }}</van-button>
                        </div>
                    </card>
                </template>
            </van-list>
        </div>
        <popup-search-index-bar
            v-model="popupSearchShow"
            :data="popupSearchData"
            displayField = "label"
            :isSearchKeyboard="true"
            @search="searchData"
            @selectedItem="selectedItem">
        </popup-search-index-bar>
        <area-popup
            :region="chooseAreaObj"
            :areaShow="popupAreaShow"
            @onAreaConfirm="onAreaConfirm"
            @onAreaCancel="onAreaCancel">
        </area-popup>
    </div>
</template>

<script>
import { reactive, toRefs, onMounted, ref } from 'vue';
import card from '@/components/_common/card.vue';
import LineCell from '@/components/_common/LineCell.vue';
import { deviceviewSetup } from '@/setup/deviceview/deviceviewSetup.js';
import { useRouter } from 'vue-router';
import Cookies from 'js-cookie';
import PopupSearchIndexBar from '@/components/_common/PopupSearchIndexBar.vue';
import { selfI18n } from '@/i18n/index';
import DeviceStatus from './manage/deviceStatus.vue';
import AreaPopup from '@/components/_common/areaPopup.vue';

export default {
    components: {
        card,
        LineCell,
        PopupSearchIndexBar,
        DeviceStatus,
        AreaPopup
    },
    setup (props, ctx) {
        const { getDevice, getAllCustomers, getProductarchiveByparamNoPage } = deviceviewSetup();
        const router = useRouter();
        const $st = selfI18n;
        const areapicker = ref(null);

        const state = reactive({
            filterdata: {
                country: '',
                province: '',
                city: '',
                params: '',
                maintainer: '',
                customer_id: '',
                n: 20,
                p: 1
            },
            filterdatareset: {
                country: '',
                province: '',
                city: '',
                params: '',
                maintainer: '',
                customer_id: '',
                n: 20,
                p: 1
            },
            loading: false,
            finished: false,
            listData: [],
            allnum: 0,
            listprops: [{
                id: 'devalias',
                label: $st('wp-she-bei-bie-ming')
            }, {
                id: 'manufacturingCode',
                label: $st('wp-chu-chang-bian-hao')
            }, {
                id: 'uuid',
                label: $st('wp-she-bei-hao')
            }, {
                id: 'customer_name',
                label: $st('wp-suo-shu-ke-hu')
            }, {
                id: 'maintainer',
                label: $st('wp-wei-bao-fu-ze-ren')
            }],
            tabArr: [{
                label: $st('wp-wo-de-she-bei'),
                value: 'my'
            }, {
                label: $st('wp-suo-you-she-bei'),
                value: 'all'
            }],
            currentTab: 'all',
            popupSearchShow: false,
            popupSearchData: [],
            chooseCustomer: null,
            popupAreaShow: false,
            chooseArea: '',
            chooseAreaObj: null
        });

        // 滚动加载下一页
        const onLoad = () => {
            state.filterdata.p++;
            getData();
        };

        // 获取数据
        const getData = () => {
            getDevice(state.filterdata).then((data) => {
                state.loading = false;
                state.allnum = data.allnum;
                console.log(state.filterdata.p * state.filterdata.n);
                if (state.filterdata.p * state.filterdata.n >= state.allnum) {
                    state.finished = true;
                }
                const dataValue = data.value || [];
                state.listData = state.listData.concat(dataValue);
            });
        };

        // 跳转到设备详情
        const toDetail = (item) => {
            router.push({
                path: '/deviceView',
                query: {
                    uuid: item.uuid,
                    customer_id: item.customer_id,
                    product_id: item.product_id,
                    devalias: item.devalias,
                    manufacturecode: item.manufacturingCode
                }
            });
        };

        // 切换标签
        const chooseTab = (value) => {
            state.currentTab = value;
            const tabValueObj = {
                my: Cookies.get('username'),
                all: ''
            };
            resetData();
            state.filterdata.maintainer = tabValueObj[value];
            state.listData = [];
            getData();
        };

        // 选择客户或者搜索选择设备
        const selectedItem = (item) => {
            if (item.type === 'customer') {
                state.filterdata.p = 1;
                state.filterdata.params = '';
                state.filterdata.customer_id = item.customer_id;
                state.listData = [];
                getData();
                state.chooseCustomer = item;
            } else if (item.type === 'device') {
                resetData();
                state.filterdata.params = item.product_id || item.uuid;
                state.listData = [];
                getData();
            }
        };

        // 搜索
        const searchData = (text) => {
            resetData();
            state.filterdata.params = text;
            state.listData = [];
            getData();
            state.popupSearchShow = false;
        };

        // 重置参数
        const resetData = () => {
            state.filterdata = {
                ...state.filterdatareset
            };
            state.chooseCustomer = null;
            state.chooseArea = '';
            state.chooseAreaArr = null;
        };

        // 获取客户
        const clickCustomerShow = () => {
            getAllCustomers(null).then((data) => {
                state.popupSearchData = data.value.map((child) => {
                    if (child.customer_id === 'notdefine') {
                        return {
                            ...child,
                            type: 'customer',
                            customer_name: $st('Unknown_owned_customer'),
                            value: $st('Unknown_owned_customer'),
                            label: $st('Unknown_owned_customer')

                        };
                    }
                    if (child.customer_id === 'nopa' && state.currentTab === 'all') {
                        return {
                            ...child,
                            type: 'customer',
                            customer_name: $st('Device_without_archive'),
                            value: $st('Device_without_archive'),
                            label: $st('Device_without_archive')
                        };
                    }
                    return {
                        value: child.customer_name,
                        label: child.customer_name,
                        type: 'customer',
                        ...child
                    };
                });
                state.popupSearchShow = true;
            });
        };

        // 展示选择地区弹窗
        const areaPopopShow = () => {
            state.popupAreaShow = true;
        };

        // 确认选中地区
        const onAreaConfirm = (area) => {
            state.filterdata.p = 1;
            state.filterdata.params = '';
            state.listData = [];
            state.filterdata.country = area.country;
            state.filterdata.province = area.province;
            state.filterdata.city = area.city;
            state.chooseAreaObj = area;
            state.chooseArea = state.filterdata.country + state.filterdata.province + state.filterdata.city;
            getData();
            state.popupAreaShow = false;
        };

        // 关闭选择地区弹窗
        const onAreaCancel = () => {
            state.popupAreaShow = false;
        };

        // 点击出搜索弹窗
        const clickSearchShow = () => {
            getProductarchiveByparamNoPage({
                params: '',
                maintainer: ''
            }).then((data) => {
                const dataValue = data.value || [];
                state.popupSearchData = dataValue.map((child) => {
                    let label = '';
                    if (child.uuid && child.devalias) {
                        label = child.devalias + '(' + child.uuid + ')';
                    } else if (child.devalias) {
                        label = child.devalias;
                    } else if (child.uuid) {
                        label = child.uuid;
                    }
                    return {
                        value: label,
                        label: label,
                        type: 'device',
                        ...child
                    };
                });
                state.popupSearchShow = true;
            });
        };

        // 删除选中客户
        const delCustomer = () => {
            state.filterdata.p = 1;
            state.chooseCustomer = null;
            state.filterdata.customer_id = '';
            state.listData = [];
            getData();
        };

        // 删除选中地区
        const delArea = () => {
            state.filterdata.p = 1;
            state.chooseArea = '';
            state.filterdata.country = '';
            state.filterdata.province = '';
            state.filterdata.city = '';
            state.listData = [];
            state.chooseAreaObj = null;
            getData();
        };

        onMounted(() => {
            getData();
        });

        return {
            ...toRefs(state),
            areapicker,
            onLoad,
            getData,
            toDetail,
            chooseTab,
            selectedItem,
            areaPopopShow,
            clickSearchShow,
            clickCustomerShow,
            onAreaConfirm,
            onAreaCancel,
            delCustomer,
            delArea,
            searchData
        };
    }
};
</script>

<style scoped lang="scss">
.list-container {
    overflow: hidden;
    background-attachment: fixed;
    background: linear-gradient(
        180deg,
        #3875c6,
        rgba(56, 117, 198, 0) 70%,
        rgba(56, 117, 198, 0) 100%);

    .list-header-box {
        padding: 16px 15px 15px;
        color: #fff;
        .list-tab-box {
            display: flex;
            justify-content: space-between;
            .list-tab {
                display: flex;
                font-size: 15px;
                align-items: baseline;
                .tab {
                    &:first-of-type {
                        margin-right: 15px;
                    }
                }
                .active {
                    font-size: 18px;
                    border-bottom: 3px #61cfbe solid;
                    padding-bottom: 2px;
                }
            }
            .list-search {
                font-size: 14px;
                align-self: flex-end;
                padding-bottom: 6px;
            }
        }
        .list-filter {
            margin-top: 15px;
            display: flex;
            font-size: 13px;
            .filter-select {
                border: 1px solid rgba(255,255,255,0.20);
                border-radius: 13px;
                height: 25px;
                line-height: 25px;
                padding-left: 15px;
                padding-right: 8px;
                flex: 1;
                &:nth-of-type(1) {
                    margin-right: 5px;
                }
                .select-cutomer {
                    margin-right: 5px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    div {
                        flex: 1;
                        width: 0;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                    }
                }
                .select-area {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    div {
                        flex: 1;
                        width: 0;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                    }
                }
            }
        }
    }

    .list-content {
        padding: 0 15px;
        z-index: -1;
        height: calc(100vh - 99px);
        overflow: scroll;
        .list-card {
            margin-bottom: 10px;
            .opt-box {
                padding-top: 5px;
                text-align: right;
                .btn {
                    height: 30px;
                    border-radius: 5px;
                    border: 1px solid #1890ff;
                }
            }
            .title-box {
                font-size: 13px;
                padding: 8px 15px 6px;
                border-bottom: 1px solid #e5e5e5;
                display: flex;
                align-items: center;
                .status {
                    width: 36px;
                    height: 36px;
                    border: 1px solid #64bb5c;
                    line-height: 36px;
                    text-align: center;
                    margin-right: 10px;
                    border-radius: 50%;
                    color: #64bb5c;
                }
                .title {

                }
            }
        }
    }
}
.line-box {
    display: flex;
    font-size: 13px;
    margin-bottom: 10px;
    .line-left {
        width: 80px;
        margin-right: 10px;
        color: rgba(0, 0, 0, 0.6);
    }
    .line-right {
        flex: 1;
    }
}
</style>
